package com.zc.container.spring;

import com.zc.container.api.mai.Container;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by zhangchi9 on 2016/12/5.
 */
public class SpringContainer implements Container{

    public static final String SPRING_CONFIG = "dubbo.spring.config";

    public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";
    static ClassPathXmlApplicationContext context;
    public static ClassPathXmlApplicationContext getContext() {
        return context;
    }
    @Override
    public void start() {
        //String configPath = ConfigUtils.getProperty(SPRING_CONFIG);
        String configPath = null;
        if (configPath == null || configPath.length() == 0) {
            configPath = DEFAULT_SPRING_CONFIG;
        }
        context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+"));
        context.start();
    }

    @Override
    public void stop() {
        try {
            if (context != null) {
                context.stop();
                context.close();
                context = null;
            }
        } catch (Throwable e) {
           // logger.error(e.getMessage(), e);
        }
    }
}
